﻿using Microsoft.Win32;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tools.Core;
using Tools.Core.Dtos;
using Tools.Core.Extensions;
using Tools.Core.Mvvm;

namespace Tools.Modules.Navigations.ViewModels
{
    public class VideoConvertViewModel : BindableBase,INavigationAware
    {
        private string _videoOutput;
        public string VideoOutput
        {
            get => _videoOutput;
            set=>SetProperty(ref _videoOutput, value);
        }
        private ObservableCollection<string> _videoTypes;
        public ObservableCollection<string> VideoTypes
        {
            get => _videoTypes;
            set=>SetProperty(ref _videoTypes, value);
        }
        private string _targetType=string.Empty;
        public string TargetType
        {
            get => _targetType;
            set=>SetProperty(ref _targetType, value);
        }
        private ObservableCollection<string> _videos=new ObservableCollection<string>();
        public ObservableCollection<string> Videos
        {
            get => _videos;
            set=>SetProperty(ref _videos, value);
        }
        private ObservableCollection<VideoConvertPlanDto> _videoPlans=new ObservableCollection<VideoConvertPlanDto>();
        public ObservableCollection<VideoConvertPlanDto> VideoPlans
        {
            get => _videoPlans;
            set=>SetProperty(ref _videoPlans, value);
        }
        public DelegateCommand ChooseVideosCommand { get; private set; }
        public DelegateCommand ChangeVideoOutputCommand { get; private set; }
        public DelegateCommand ConvertCommand { get; private set; }
        public VideoConvertViewModel(GlobalConfig config)
        {
            VideoTypes = config.VideoTypes;
            VideoOutput = config.VideoOutput;
            ChooseVideosCommand = new DelegateCommand(() =>
            {
                var ofd=new Microsoft.Win32.OpenFileDialog();
                ofd.Multiselect = true;
                if (ofd.ShowDialog() == true)
                {
                    Videos = ofd.FileNames.AsObservableCollection();
                }
            });
            ChangeVideoOutputCommand = new DelegateCommand(() =>
            {
                var fbd = new FolderBrowserDialog();
                if (fbd.ShowDialog() == DialogResult.OK)
                {
                    VideoOutput = fbd.SelectedPath;
                    Kits.WriteObject(config);
                }
            });
            ConvertCommand = new DelegateCommand(() => {
                var addPlans = Videos.Select(video => {
                    FileInfo f = new FileInfo(video);
                    return new VideoConvertPlanDto
                    {
                        FromPath = f.FullName,
                        Name = f.Name,
                        TargetType=TargetType,
                        OutputDir = VideoOutput,
                        CreateTime = DateTime.Now
                    };
                }).AsObservableCollection();
                VideoPlans=VideoPlans.Concat(addPlans).AsObservableCollection();
                TasksQueueHelper.GetQueue("VideoConvert").Enqueue(() =>
                {
                    Softwares.ConvertVideos(addPlans);
                    return Task.CompletedTask;
                });
            },()=>Videos.Count>0&&TargetType!=string.Empty).ObservesProperty(()=>TargetType).ObservesProperty(()=>Videos);
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
        }
    }
}
